package com.javaspeak.interviewquestions.multipleanalyser;

/**
 * Class used to answer question:
 * <p>
 * 1) Write a program that prints the numbers from 1 to 100.
 * </p>
 * <p>
 * For multiples of 3, print "Peek", instead of the number<br/>
 * For multiples of 5, print "Boo", instead of the number<br/>
 * For multiples of 3 and 5, print "Peek a Boo", instead of the number 
 * </p>
 * @author John Dickerson
 */
public class MultipleAnalyzer {

	private static String[] cache = new String[ 101 ];
	
	static {
		populateCache();
	}
		
	/**
	 * Populates the cache on initialisation with known multiple Strings: 
	 * "Boo", "Peek" and "Peek a Boo".
	 * <p>
	 * "Peek" is held if the index of the array is a multiple of 3 but not a 
	 * multiple of both 3 and 5. "Boo" is held if the index is a multiple of 
	 * 5 but not a multiple of both 3 and 5. "Peek a Boo" is held if the index 
	 * is a multiple of both 3 and 5.
	 * </p>
	 */
	public static void populateCache(){
				
		int y = 100 / 3;
		
		for( int i=1; i<y+1; i++ ){
			
			cache[ i * 3  ] = "Peek";
		}
		
		y = 100 / 5;
		int multiple;
		
		for( int i=1; i<y+1; i++ ){
			
			multiple = i * 5;
			
			if ( cache[ multiple ] == null ){
				
				cache[ multiple ] = "Boo";
				
			}
			else {
				
				cache[ multiple ] = "Peek a Boo";
			}
		}
	}
	
	
	/**
	 * Returns cache. Note the cache will only contain multiple Strings,
	 * "Boo", "Peek" and "Peek a Boo" on initialisation but after the 
	 * printNumbers() method has been called will also contain numbers equal 
	 * to the index of the array. The first index 0 of the cache is not used.
	 * 
	 * @returns cache
	 */
	public static String[] getCache(){
		
		return cache;
	}
	
	
	/**
	 * Prints the numbers or multiple Strings  
	 */
	public static void printNumbersAndMultipleStrings(){
		
		for ( int i=1; i<101; i++ ){
			
			if ( cache[ i ] == null ){
				
				cache[ i ] = i + "";
			}
			
			System.out.println( cache[ i ] );
		}
	}
	
	
	public static void main( String args[] ){
		
		printNumbersAndMultipleStrings();
	}
}
